#include "fensaisieeleve.h"
#include "ui_fensaisieeleve.h"

FenSaisieEleve::FenSaisieEleve(QWidget *parent) :
    QDialog(parent)

{
    setupUi(this);

    QObject::connect(buttonBox,SIGNAL(accepted()),this,SLOT(BtnOK()));
    QObject::connect(buttonBox,SIGNAL(rejected()),this,SLOT(BtnCancel()));
}
FenSaisieEleve::~FenSaisieEleve()
{
    delete this;
}
void FenSaisieEleve::SetData(QStringList listeInstru, ListeTypeCours listeTypeCours, ListeProf listeProf, ListeEleve &listeEleve, bool modifierEleve, int indexEleve, Eleve eleve)
{
    m_ListeTypeCours = listeTypeCours;
    m_ListeProf = listeProf;
    m_ListeEleve = &listeEleve;

    QStringList sListeTypeCours;
    for (int i = 0; i < listeTypeCours.count(); i++)
    {
        sListeTypeCours << listeTypeCours.at(i).DureeEtNbEleves();
    }
    QStringList sListeProf;
    for (int i = 0; i < listeProf.count(); i++)
    {
        sListeProf << listeProf.at(i).getInfo().NomEtPrenom();
    }

    instrumentComboBox->addItems(listeInstru);
    typeDuCoursComboBox->addItems(sListeTypeCours);
    profParDFautComboBox->addItems(sListeProf);
    QStringList ListeJourSemaine = (QStringList() << QDate::longDayName(1) << QDate::longDayName(2) << QDate::longDayName(3) << QDate::longDayName(4) << QDate::longDayName(5) << QDate::longDayName(6) << QDate::longDayName(7));
    jourComboBox->addItems(ListeJourSemaine);

    ModeSaisie = modifierEleve;
    if (ModeSaisie)
        ModifierEleveIsON(indexEleve, eleve);
}

//void FenSaisieEleve::receptionListeChargementComboBox(QStringList li , ListeTypeCours ltpc, ListeProf lpf, ListeEleve le)
//{
//    maListeTypeCours = ltpc;
//    maListeProf = lpf;
//    maListeEleve = le;

//    QStringList sListeTypeCours;
//    for (int i = 0; i < ltpc.count(); i++)
//    {
//        sListeTypeCours << ltpc.at(i).DureeEtNbEleves();
//    }
//    QStringList sListeProf;
//    for (int i = 0; i < lpf.count(); i++)
//    {
//        sListeProf << lpf.at(i).getInfo().NomEtPrenom();
//    }
//    instrumentComboBox->addItems(li);
//    typeDuCoursComboBox->addItems(sListeTypeCours);
//    profParDFautComboBox->addItems(sListeProf);
//    QStringList ListeJourSemaine = (QStringList() << QDate::longDayName(1) << QDate::longDayName(2) << QDate::longDayName(3) << QDate::longDayName(4) << QDate::longDayName(5) << QDate::longDayName(6) << QDate::longDayName(7));
//    jourComboBox->addItems(ListeJourSemaine);
//}
void FenSaisieEleve::BtnOK()
{
    if (!nomLineEdit->text().isEmpty() &&  !prNomLineEdit->text().isEmpty())
    {

        Info i1(nomLineEdit->text(),prNomLineEdit->text(),adresseLineEdit->text(),codePostalLineEdit->text(),villeLineEdit->text(),tLPhoneFixeLineEdit->text(),tLPhonePortableLineEdit->text(),eMailLineEdit->text());

        QString sJour = jourComboBox->currentText();
        int jour = 1;
        for (int i = 1; i <= 7; i++)
        {
            if (sJour == QDate::longDayName(i))
                jour = i + 3;
        }

        QTime horaire(horaireTimeEdit->time());
        QDate date(2010, 1, jour);

        TypeCours tpc;
        tpc = m_ListeTypeCours.getTypeDeCours(typeDuCoursComboBox->currentText());
        Prof pf;
        pf = m_ListeProf.getProf(profParDFautComboBox->currentText());

        Cours co(date, horaire, jourComboBox->currentText(), tpc, pf, instrumentComboBox->currentText());

        ListeCours lc;
        ListePaiement lp;

        if (ModeSaisie)
        {
            lc = EleveAModifier.getListeCours();
            lp = EleveAModifier.getlistePaiement();
        }

        Eleve eleve(i1, lc, lp,co, pf, AdultecheckBox->isChecked(), MotteraincheckBox_2->isChecked(),true, TypeDeCotisationspinBox->value());


        if (ModeSaisie)
        {
            eleve.setCotis(EleveAModifier.getCotis());
            if (m_ListeEleve->count() >= LIndex)
            {
                m_ListeEleve->replace(LIndex,eleve);
            }
        }
        else
        {
            if (!m_ListeEleve->isEmpty())
            {
                if (m_ListeEleve->Existe(eleve.getInfo().NomEtPrenom()))
                {
                    QMessageBox::information(NULL,ERREUR, CETELEVEEXISTEDEJA);
                }
                else
                {
                    m_ListeEleve->append(eleve);
                }
            }
            else
            {
               m_ListeEleve->append(eleve);
            }
        }
    }
    else
    {
        QMessageBox::information(NULL, ERREUR, VOUSDEVEZOBLIGATOIREMENTSAISIRUNNOMUNPRENOM);
        return;
    }
    this->close();
}
void FenSaisieEleve::BtnCancel()
{
    this->close();


}
void FenSaisieEleve::ModifierEleveIsON(int monIndex, Eleve eleve)
{
    EleveAModifier = eleve;
    nomLineEdit->setText(eleve.getInfo().getNom());
    prNomLineEdit->setText(eleve.getInfo().getPrenom());
    adresseLineEdit->setText(eleve.getInfo().getAdresse());
    codePostalLineEdit->setText(eleve.getInfo().getCodePostal());
    villeLineEdit->setText(eleve.getInfo().getVille());
    tLPhoneFixeLineEdit->setText(eleve.getInfo().getTelFixe());
    tLPhonePortableLineEdit->setText(eleve.getInfo().getTelPortable());
    eMailLineEdit->setText(eleve.getInfo().getEmail());
    AdultecheckBox->setChecked(eleve.getAdulte());
    MotteraincheckBox_2->setChecked((eleve.getMotterain()));
    TypeDeCotisationspinBox->setValue(eleve.getTypeCotis());
    horaireTimeEdit->setTime(eleve.getCoursParDefaut().getHoraire());

    QString sProf = eleve.getProfPrincipal().getInfo().NomEtPrenom();
    QString sJour = eleve.getCoursParDefaut().getJour();
    QString sTypeDeCours = eleve.getCoursParDefaut().gettypeDuCours().DureeEtNbEleves();
    QString sInstrument = eleve.getCoursParDefaut().getInstrument();

    for (int i = 0 ; i < profParDFautComboBox->count(); i++)
    {
        if (profParDFautComboBox->itemText(i) == sProf)
            profParDFautComboBox->setCurrentIndex(i);
    }

    for (int i = 0 ; i < jourComboBox->count(); i++)
    {
        if (jourComboBox->itemText(i) == sJour)
            jourComboBox->setCurrentIndex(i);
    }
    for (int i = 0 ; i < typeDuCoursComboBox->count(); i++)
    {
        if (typeDuCoursComboBox->itemText(i) == sTypeDeCours)
            typeDuCoursComboBox->setCurrentIndex(i);
    }
    for (int i = 0 ; i < instrumentComboBox->count(); i++)
    {
        if (instrumentComboBox->itemText(i) == sInstrument)
            instrumentComboBox->setCurrentIndex(i);
    }
    ModeSaisie = true;
    LIndex = monIndex;


}
